package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.city_home_app.Activity.WebViewActivity;
import com.example.city_home_app.Bean.Down_News;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;


import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_News extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Down_News> list;
    private Context context;

    public Adapter_News(Context context) {
        this.context = context;
    }

    public void setList(List<Down_News> alist) {
        this.list = alist;
    }

    public void clearList() {
        if(list != null){
            this.list.clear();
        }
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_news, parent, false);
        Adapter_News.ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final Down_News bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_News.ViewHolder viewHolder = (Adapter_News.ViewHolder) holder;

        String[] imgs = bean.getNews_images().split(",");

        viewHolder.tvRecNewsauthor.setText(bean.getNews_author());
        viewHolder.tvRecNewsclick.setText(bean.getNews_hit() + "阅读");
        viewHolder.tvRecNewstime.setText(bean.getNews_time());
        viewHolder.tvRecNewstitle.setText(bean.getNews_title());

        if (imgs.length >= 3) {
            Picasso.with(context)
                    .load(imgs[0])
                    .into(viewHolder.imgRecNewsimg1);
            Picasso.with(context)
                    .load(imgs[1])
                    .into(viewHolder.imgRecNewsimg2);
            Picasso.with(context)
                    .load(imgs[2])
                    .into(viewHolder.imgRecNewsimg3);
        } else if (imgs.length >= 2) {
            Picasso.with(context)
                    .load(imgs[0])
                    .into(viewHolder.imgRecNewsimg1);
            Picasso.with(context)
                    .load(imgs[1])
                    .into(viewHolder.imgRecNewsimg2);
            viewHolder.imgRecNewsimg3.setVisibility(View.INVISIBLE);
        } else if (imgs.length >= 1 && !imgs[0].equals("")) {
            Picasso.with(context)
                    .load(imgs[0])
                    .into(viewHolder.imgRecNewsimg1);
            viewHolder.imgRecNewsimg2.setVisibility(View.INVISIBLE);
            viewHolder.imgRecNewsimg3.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.imgRecNewsimg1.setVisibility(View.INVISIBLE);
            viewHolder.imgRecNewsimg2.setVisibility(View.INVISIBLE);
            viewHolder.imgRecNewsimg3.setVisibility(View.INVISIBLE);
        }

        final RetrofitListener<Down_Universe> retrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if(listener.getMessage().equals("success")){
                    Intent intent = new Intent(context, WebViewActivity.class);
                    intent.putExtra("url",bean.getNews_link());
                    intent.putExtra("id",bean.getNews_id());
                    intent.putExtra("count",bean.getNews_comment());
                    context.startActivity(intent);
                }else{
                    UIUtils.showToast(context,"网络出了点问题",UIUtils.LENGTH_SHORT);
                }
            }

            @Override
            public void onFail(String msg) {
                UIUtils.showToast(context,"网络出了点问题",UIUtils.LENGTH_SHORT);
            }
        };

        viewHolder.layoutRecNews.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    viewHolder.layoutRecNews.setBackgroundColor(0xffededed);
                } else if (
                        motionEvent.getAction() == MotionEvent.ACTION_OUTSIDE ||
                                motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {
                    viewHolder.layoutRecNews.setBackgroundColor(0xffffffff);
                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    viewHolder.layoutRecNews.setBackgroundColor(0xffffffff);
                    ApiModel model = new ApiModel();
                    model.addnewsHot(bean.getNews_id(),retrofitListener);
                }
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_rec_newstitle)
        TextView tvRecNewstitle;
        @BindView(R.id.img_rec_newsimg1)
        ImageView imgRecNewsimg1;
        @BindView(R.id.img_rec_newsimg2)
        ImageView imgRecNewsimg2;
        @BindView(R.id.img_rec_newsimg3)
        ImageView imgRecNewsimg3;
        @BindView(R.id.tv_rec_newsauthor)
        TextView tvRecNewsauthor;
        @BindView(R.id.tv_rec_newsclick)
        TextView tvRecNewsclick;
        @BindView(R.id.tv_rec_newstime)
        TextView tvRecNewstime;
        @BindView(R.id.layout_rec_news)
        ConstraintLayout layoutRecNews;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
